package com.tuishan.myapplication.Views.Users;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.tuishan.myapplication.Adapters.HomeAdapter;
import com.tuishan.myapplication.Custom.LoadingView;
import com.tuishan.myapplication.Model.PageNumModel;
import com.tuishan.myapplication.Model.ProductModel;
import com.tuishan.myapplication.R;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;

import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import okhttp3.Request;
import okhttp3.Response;

public class UserCollectionActivity extends Activity implements OnTitleBarListener {
    LoadingView Loading;
    TitleBar titleBar;
    Context mContext;
    RecyclerView user_collection_recyclerView;
    HomeAdapter adapter;
    List<ProductModel> productModelList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_collection);
        mContext = this;
        Loading = new LoadingView(mContext);
        titleBar = findViewById(R.id.titleBar);
        user_collection_recyclerView = findViewById(R.id.user_collection_recyclerView);
        titleBar.setOnTitleBarListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        user_collection_recyclerView.setLayoutManager(layoutManager);
        adapter = new HomeAdapter(productModelList, Loading, this, false);
        user_collection_recyclerView.setAdapter(adapter);
        GetTransList(1, 200);
    }

    private void GetTransList(int pageNum, int pageSize){
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("pageNum", pageNum);
        objectMap.put("pageSize", pageSize);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.GetUserCollectionTransactUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object arg1) {
                Loading.dismiss();
                Gson gson = new Gson();
                PageNumModel<ProductModel> pageNumModel = gson.fromJson(arg1.toString(),
                        new TypeToken<PageNumModel<ProductModel>>() {
                        }.getType());
                productModelList.clear();
                productModelList.addAll(pageNumModel.getList());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Loading.dismiss();
                Toast.makeText(mContext, err_msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Loading.dismiss();
            }
        });
    }


    @Override
    public void onLeftClick(View v) {
        finish();
    }

    @Override
    public void onTitleClick(View v) {

    }

    @Override
    public void onRightClick(View v) {

    }
}
